package util
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.utils.Dictionary;
	
	/**
	 * 纹理集合
	 * @author WLDragon 2013-12-22
	 */
	public class Textures
	{
		
		public function Textures()
		{
			mLib = new Dictionary;
			mTypes = new Dictionary;
			//mTypes[1] = new <int>[53,54,55,63,64,66,67];//山
			//mTypes[2] = new <int>[11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,51,52,61,62];//水
			//mTypes[3] = new <int>[47,48,56,57,58,65,68];//平原
			//mTypes[4] = new <int>[15,16,17,18,25,26,27,28,35,36,37,38,44,45,46];//森林
			//mTypes[5] = new <int>[];//沙漠
		}
		
		public function getType($val:int):int 
		{
			for (var i:int = 1; i < typeLength;i++ )
			{
				if (mTypes[i].indexOf(String($val)) != -1)
				{
					return i;
				}
			}
			
			return 0;
		}
		
		/**
		 * 初始化纹理集
		 * @param	$name 资源路径，使用morn打包的图片资源
		 * @param	$px 横向切片数量,//**********************应该改为提供单位纹理的长和宽来切好点
		 * @param	$py 纵向切片数量
		 */
		public function initTexture($name:String, $px:int, $py:int):void
		{
			var d:BitmapData = App.asset.getBitmapData($name);
			if (d != null)
			{
				_w = d.width / $px;
				_h = d.height / $py;
				
				//把图片切片保存
				var mt:Matrix = new Matrix();
				for (var i:int = 0; i < $py; i++)
				{
					for (var j:int = 0; j < $px; j++)
					{
						mt.tx = -j * _w;
						mt.ty = -i * _h;
						var bd:BitmapData = new BitmapData(_w, _h, false);
						bd.draw(d, mt);
						mLib[10 * (i+1) + (j+1)] = bd;
					}
				}
			}
			
			config = new XML(App.loader.getResLoaded("assets/config.xml"));
			typeLength = config.texture.type.length();
			for each(var x:XML in config.texture.type)
			{
				mTypes[int(x.@id)] = String(x.@index).split(',');
			}
		}
		
		/**
		 * 获取纹理资源
		 * 按从左到右，从上到下的序号为
		 * 11 12 13 ...
		 * 21 22 ...
		 * ...
		 * @param	$index 序号
		 * @return
		 */
		public function getData($index:int):BitmapData
		{
			return mLib[$index];
		}
		
		/**单个纹理的宽度*/
		public function get w():int 
		{
			return _w;
		}
		
		/**单个纹理的高度*/
		public function get h():int 
		{
			return _h;
		}
		
		private var mLib:Dictionary;
		/**单个纹理的宽度*/
		private var _w:int;
		/**单个纹理的高度*/
		private var _h:int;
		/**类型定义字典，键所表示的类型集 # 1-山 2-水 3-平原 4-森林 5-沙漠*/
		private var mTypes:Dictionary;
		/**配置文件*/
		private var config:XML;
		/**地图类别长度*/
		private var typeLength:int;
		
		/**纹理集的列数，因为这里暂时只处理地图的纹理，所以直接静态写死，如果在rewar项目中要用到其他纹理再改进，把该类改多纹理集库*/
		public static const COL:int = 9;
		/**纹理集的行数*/
		public static const ROW:int = 9;
	}

}